//
//  WJYSpeicalViewController.m
//  WWLNetease
//
//  Created by dllo on 16/1/4.
//  Copyright © 2016年 Echo.Jobs. All rights reserved.
//

#import "WJYSpeicalViewController.h"

@interface WJYSpeicalViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *specialTableView;
@property (nonatomic, retain) NSMutableArray *sectionArr;
@property (nonatomic, retain) NSMutableArray *rowArr;
@property (nonatomic, retain) NSMutableArray *tempArr;

@end

@implementation WJYSpeicalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = self.specialTitle;
    
    [self handleData];
    [self creatSpecialTableView];
    
}

- (void)handleData
{
    
    NSString *specialStr = [NSString stringWithFormat:@"http://c.m.163.com/nc/special/%@.html", self.specialID];
    
    [AppTools getDataWithURL:specialStr Block:^(id result) {
        
        NSDictionary *dic = [result objectForKey:self.specialID];
        _sectionArr = [NSMutableArray array];
        _tempArr = [NSMutableArray array];
        
        NSArray *array = [dic objectForKey:@"topics"];
        for (NSDictionary *dic in array) {
            WJYNewsModel *speicalNewsModel = [[WJYNewsModel alloc] initWithDictionary:dic];
            [_sectionArr addObject:speicalNewsModel];
            NSArray *arr = [dic objectForKey:@"docs"];
            
            _rowArr = [NSMutableArray array];
            for (NSDictionary *dicc in arr) {
                WJYNewsModel *contentModel = [[WJYNewsModel alloc] initWithDictionary:dicc];
                [_rowArr addObject:contentModel];
                
            }
            [_tempArr addObject:_rowArr];
            
        }
        [_specialTableView reloadData];
        
    } ErrorBlock:^(NSError *error) {
        
        
    }];
    
}

-(void)creatSpecialTableView
{
    _specialTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_FRAME.size.width, SCREEN_FRAME.size.height) style:UITableViewStylePlain];
    _specialTableView.delegate = self;
    _specialTableView.dataSource = self;
    _specialTableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:_specialTableView];
    
    [_specialTableView registerClass:[WJYNewsTableViewCell class] forCellReuseIdentifier:@"newsTableViewCell"];
    [_specialTableView registerClass:[WJYThreePctureTableViewCell class] forCellReuseIdentifier:@"newImagesTableViewCell"];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return SCREEN_FRAME.size.width / 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *arr = [_tempArr objectAtIndex:section];
    return arr.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _sectionArr.count;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    WJYNewsModel *speicalNewsModel = [_sectionArr objectAtIndex:section];
    NSString *tname = [NSString stringWithFormat:@"%ld/%ld %@", section + 1, _sectionArr.count, speicalNewsModel.tname];
    return tname;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *tempArr = [_tempArr objectAtIndex:indexPath.section];
    WJYNewsModel *topLineModel = [tempArr objectAtIndex:indexPath.row];
    if (topLineModel.imgextra != nil) {
        WJYThreePctureTableViewCell *newImagesTableViewCell = [tableView dequeueReusableCellWithIdentifier:@"newImagesTableViewCell"];
        newImagesTableViewCell.newsM = topLineModel;
        return newImagesTableViewCell;
    } else {
        WJYNewsTableViewCell *newsTableViewCell = [tableView dequeueReusableCellWithIdentifier:@"normalNewsViewControlle"];
        newsTableViewCell.newsM = topLineModel;
        return newsTableViewCell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 刚选中又马上取消选中，格子不变色
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSArray *tempArr = [_tempArr objectAtIndex:indexPath.section];
    WJYNewsModel *topLineModel = [tempArr objectAtIndex:indexPath.row];
    if ([topLineModel.skipType isEqualToString:@"photoset"]) {
        
//        LouPhotographViewController *photoVC = [[LouPhotographViewController alloc] init];
//        photoVC.photosetID = topLineModel.photosetID;
//        photoVC.photoTitle = topLineModel.title;
//        [self.navigationController pushViewController:photoVC animated:YES];
        
    } else if ([topLineModel.skipType isEqualToString:@"live"]) {
        
    } else {
        
        NewsViewController *normalNewsVC = [[NewsViewController alloc] init];
        normalNewsVC.docId = topLineModel.docid;
        normalNewsVC.newsTitle = topLineModel.title;
        [self.navigationController pushViewController:normalNewsVC animated:YES];
        
    }
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
